﻿Public Class ConfValidator

    Protected Shared validationMessage As String

    Public Shared Function IsConfValid(ByVal xmlFile As String, ByVal xsdFile As String) As String
        Dim reader As Xml.XmlTextReader = Nothing
        Dim SR As System.IO.StreamReader = Nothing

        Try
            reader = New Xml.XmlTextReader(xmlFile)
            SR = New System.IO.StreamReader(xsdFile)
            Dim schema As New Xml.Schema.XmlSchema()
            schema = Xml.Schema.XmlSchema.Read(SR, AddressOf validator)
            Dim readerSettings As New Xml.XmlReaderSettings()
            readerSettings.ValidationType = Xml.ValidationType.Schema
            readerSettings.Schemas.Add(schema)
            AddHandler readerSettings.ValidationEventHandler, AddressOf validator
            Dim objXmlReader As Xml.XmlReader = Xml.XmlReader.Create(reader, readerSettings)

            validationMessage = ""

            While objXmlReader.Read()
            End While

        Catch ex As Exception
            validationMessage = ex.ToString()
        Finally
            If (SR IsNot Nothing) Then
                SR.Close()
                SR = Nothing
            End If
            reader.Close()
            reader = Nothing
        End Try

        Return validationMessage

    End Function

    Private Shared Sub validator(ByVal sender As Object, ByVal args As Xml.Schema.ValidationEventArgs)
        validationMessage = args.Message()
    End Sub

End Class
